package com.cuicuisha.gxyp.cart.service.impl;

import com.alibaba.fastjson.JSON;
import com.cuicuisha.gxyp.cart.service.CartService;
import com.cuicuisha.gxyp.feign.product.ProductFeignClient;
import com.cuicuisha.gxyp.model.entity.cart.CartInfo;
import com.cuicuisha.gxyp.model.entity.product.ProductSku;
import com.cuicuisha.gxyp.model.entity.user.AuthContextUtil;
import com.cuicuisha.gxyp.model.entity.user.UserInfo;
import com.cuicuisha.gxyp.model.vo.common.Result;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: CartServiceImpl
 * Description:
 *
 * @Author 脆脆鲨
 * @Create 2024-09-22 22:00
 * @Version 1.0
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate  redisTemplate;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public void addToCart(Long skuId, Integer skuNum) {
        Long userId = AuthContextUtil.getUser().getId();
        // 购物车之前是否添加过  定义key user:cart:userId
        Boolean isHas = redisTemplate.opsForHash().hasKey("user:cart:" + userId, skuId + "");
        //Object cartInfoObj = redisTemplate.opsForHash().get("user:cart:" + userId, String.valueOf(skuId));
        CartInfo cartInfo = null ;
        if (isHas){
            // 修改
            cartInfo =(CartInfo) redisTemplate.opsForHash().get("user:cart:" + userId, skuId + "");
            //cartInfo = JSON.parseObject(cartInfoObj.toString() , CartInfo.class) ;
            cartInfo.setSkuNum(cartInfo.getSkuNum()+skuNum);
            cartInfo.setIsChecked(1);
            cartInfo.setUpdateTime(new Date());
        }else {
            // 添加到购物车
            cartInfo = new CartInfo();
            ProductSku productSku = productFeignClient.getBySkuId(skuId);
            cartInfo.setCartPrice(productSku.getSalePrice());
            cartInfo.setSkuNum(skuNum);
            cartInfo.setSkuId(skuId);
            cartInfo.setUserId(userId);
            cartInfo.setImgUrl(productSku.getThumbImg());
            cartInfo.setSkuName(productSku.getSkuName());
            cartInfo.setIsChecked(1);
            cartInfo.setCreateTime(new Date());
            cartInfo.setUpdateTime(new Date());
        }
        redisTemplate.opsForHash().put("user:cart:" + userId, skuId + "", cartInfo);
    }

    @Override
    public List<CartInfo> getCartList() {
        List<CartInfo> cartInfoList = (List<CartInfo>)redisTemplate.opsForHash().values("user:cart:" + AuthContextUtil.getUser().getId());
        return cartInfoList;
    }

    @Override
    public void deleteCart(Long skuId) {
        redisTemplate.opsForHash().delete("user:cart:" + AuthContextUtil.getUser().getId(), skuId + "");
    }

    @Override
    public void checkCart(Long skuId, Integer isChecked) {
        Boolean hasKey = redisTemplate.opsForHash().hasKey("user:cart:" + AuthContextUtil.getUser().getId(), skuId + "");
        if (hasKey){
            CartInfo cartInfo = (CartInfo)redisTemplate.opsForHash().get("user:cart:" + AuthContextUtil.getUser().getId(), skuId + "");
            cartInfo.setIsChecked(isChecked);
            redisTemplate.opsForHash().put("user:cart:" + AuthContextUtil.getUser().getId(), skuId + "", cartInfo);
        }
    }

    @Override
    public void allCheckCart(Integer isChecked) {
        List<CartInfo> cartInfoList = (List<CartInfo>) redisTemplate.opsForHash().values("user:cart:" + AuthContextUtil.getUser().getId());
        if (!CollectionUtils.isEmpty(cartInfoList)){
            cartInfoList.stream().map(cartInfo -> {
//                CartInfo cartInfo = JSON.parseObject(CartInfo.toString(), CartInfo.class);
                cartInfo.setIsChecked(isChecked);
                return cartInfo;
            }).forEach(cartInfo ->
                redisTemplate.opsForHash().put("user:cart:" + AuthContextUtil.getUser().getId(), String.valueOf(cartInfo.getSkuId()), cartInfo)
            );
        }
    }

    @Override
    public void clearCart() {
        redisTemplate.delete("user:cart:" + AuthContextUtil.getUser().getId());
    }

    @Override
    public List<CartInfo> getAllChecked() {
        List<CartInfo> cartInfoList = (List<CartInfo>)redisTemplate.opsForHash().values("user:cart:" + AuthContextUtil.getUser().getId());
        if (!CollectionUtils.isEmpty(cartInfoList)){
            List<CartInfo> checkedCartInfoList = cartInfoList.stream().filter(cartInfo -> cartInfo.getIsChecked() == 1).collect(Collectors.toList());
            return checkedCartInfoList;
        }
        return new ArrayList<>();
    }

    @Override
    public void deleteChecked() {
        List<CartInfo> cartInfoList = (List<CartInfo>) redisTemplate.opsForHash().values("user:cart:" + AuthContextUtil.getUser().getId());
        if (!CollectionUtils.isEmpty(cartInfoList)){
            cartInfoList.stream().filter(cartInfo -> cartInfo.getIsChecked() == 1).forEach(cartInfo ->
                redisTemplate.opsForHash().delete("user:cart:" + AuthContextUtil.getUser().getId(), String.valueOf(cartInfo.getSkuId())));
        }
    }
}














































